import 'package:flutter/material.dart';
import 'package:lz_app_core/kd_user/kd_user_info.dart';
import 'package:lz_app_core/utils/sp_util.dart';
import 'package:lz_base/kd_base.dart';
import 'package:lz_login/kd_login.dart';
import 'package:lz_router/kd_router.dart';
import 'package:lz_ui/kd_ui.dart';
import 'package:lz_form/kd_form.dart';

class KDSettingPage extends KDFormBasePage {
  final KDSettingPageVM _vm = KDSettingPageVM();

  KDSettingPage({super.key});

  @override
  setupElement(StatelessElement element) {
    _vm.loadData();
    return super.setupElement(element);
  }

  @override
  String? get title => "设置";

  @override
  KDFormBasePageVM get pageVM => _vm;

  @override
  Widget? get bottomWidget => Padding(
        padding: const EdgeInsets.only(top: 24),
        child: KDButton.bottomBtn(
            title: "退出登录",
            textColor: KDColor.title,
            bgColor: KDColor.disInputBg,
            tap: () async {
              /// 清理用户信息
              await KDLoginManager.unbindJPush();
              KDRouter.offAllNamed(KDLoginRouter.login);
              SpUtils.remove('memberId');
              KDUser.clean();
            }),
      );
}

class KDSettingPageVM extends KDFormBasePageVM {
  loadData() {
    datas.add(
      KDFormTitleWidgetVM(
        title: "修改密码",
        showArrowRight: true,
        didClick: () => KDRouter.to(KDLoginRouter.changePwd),
      ),
    );
    datas.add(
      KDFormTitleWidgetVM(
        title: "修改登录手机",
        showArrowRight: true,
        didClick: () => KDRouter.to(KDLoginRouter.changePhone),
      ),
    );

    if (KDUserPermission.check("close:account")) {
      datas.add(
        KDFormTitleWidgetVM(
          title: "注销账户",
          showArrowRight: true,
          didClick: () => KDRouter.to(KDLoginRouter.cancellation),
        ),
      );
    }
    datas.addAll([
      KDFormTitleWidgetVM(
        title: "关于蓝智科技",
        isBLine: false,
        showArrowRight: true,
        didClick: () => KDRouter.to(KDLoginRouter.aboutPage),
      ),
    ]);
    update();
  }
}
